import React from 'react';

export default function asyncImportComponent(component) {
  class AsyncComponent extends React.Component {
    state = { Component: null };

    async componentWillMount() {
      const { default: Component } = await component();
      this.setState({ Component });
    }

    render() {
      const { Component } = this.state;
      return Component
        ? <Component {...this.props} />
        : null;
    }
  }

  return AsyncComponent;
}
